package com.ruoyi.classai.api;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.classai.domain.dto.HomeworkDto;
import com.ruoyi.classai.domain.vo.homework.HomeWorkAnalysisVo;
import com.ruoyi.classai.domain.vo.homework.HomeWorkSubStudentVo;
import com.ruoyi.classai.domain.vo.homework.HomeWorkVo;
import com.ruoyi.classai.service.IEwcHomeworkService;
import com.ruoyi.classai.service.IEwcHomeworkSubmissionService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.web.controller.BaseController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * 课后作业接口
 * @author zsp
 * @date 2025年05月11日
 */
@Slf4j
@Validated
@RestController
@Tag(name = "学生端小组联系和课后作业接口")
@RequestMapping("/api/homework")
public class HomeWorkApi extends BaseController{

    @Autowired
    private IEwcHomeworkService ewcHomeworkService;

    @Autowired
    private IEwcHomeworkSubmissionService ewcHomeworkSubmissionService;

    /**
     * 查询课后作业列表
     */
    @Operation(summary = "查询课后作业列表")
    @GetMapping("/getHomeWorkList")
    public TableDataInfo getHomeWorkList(String className,String homeworkType,String deadlineStart,String deadlineEnd)
    {
        startPage();
        List<HomeWorkVo> list = ewcHomeworkService.getHomeWorkList(className,homeworkType,deadlineStart,deadlineEnd);
        return getDataTable(list);
    }

    /**
     * 查询课后作业详情
     */
    @Operation(summary = "查询课后作业详情")
    @GetMapping(value = "/getHomeWorkInfo")
    public AjaxResult getHomeWorkInfo(@NotBlank(message = "作业唯一标识不能为空")String homeworkId,
                                      @NotBlank(message = "班级唯一标识不能为空")String classId){
        HomeWorkVo homeWorkVo = ewcHomeworkService.getHomeWorkInfo(homeworkId,classId);
        List<HomeWorkSubStudentVo> homeWorkSubStudentList = null;
        if(!Objects.isNull(homeWorkVo)){
            homeWorkSubStudentList = ewcHomeworkService.getHomeWorkSubStudentList(homeworkId,classId);
        }
        JSONObject resultJson=new JSONObject();
        resultJson.put("homeWorkVo",homeWorkVo);
        resultJson.put("homeWorkSubStudentList",homeWorkSubStudentList);
        return AjaxResult.success("查询成功",resultJson );
    }

    /**
     * 查询课后作业学生提交列表
     */
    @Operation(summary = "查询课后作业学生提交列表")
    @GetMapping(value = "/getSubStudentHomeWorkList")
    public AjaxResult getSubStudentHomeWorkList(@NotBlank(message = "作业唯一标识不能为空")String homeworkId,
                                                @NotBlank(message = "班级唯一标识不能为空")String classId,
                                                String studentName,
                                                String homeworkFlag){
        HomeWorkVo homeWorkVo = ewcHomeworkService.getHomeWorkInfo(homeworkId,classId);
        List<HomeWorkSubStudentVo> homeWorkSubStudentList = null;
        if(!Objects.isNull(homeWorkVo)){
            homeWorkSubStudentList = ewcHomeworkService.getSubStudentHomeWorkList(homeworkId,classId,studentName,homeworkFlag);
        }
        JSONObject resultJson=new JSONObject();
        resultJson.put("homeWorkSubStudentList",homeWorkSubStudentList);
        return AjaxResult.success("查询成功",resultJson );
    }

    /**
     * 查询课后作业AI解析详情
     */
    @Operation(summary = "查询课后作业AI解析详情")
    @GetMapping(value = "/getHomeWorkAnalysisInfo")
    public AjaxResult getHomeWorkAnalysisInfo(@NotBlank(message = "作业唯一标识不能为空")String homeworkId,
                                              @NotBlank(message = "学生唯一标识不能为空")String studentId){
        HomeWorkAnalysisVo homeWorkAnalysisVo = ewcHomeworkService.getHomeWorkAnalysisInfo(homeworkId,studentId);
        return AjaxResult.success("查询成功",homeWorkAnalysisVo );
    }

    /**
     * 批改课后作业
     */
    @Operation(summary = "批改课后作业")
    @PostMapping(value = "/correctHomeWork")
    public AjaxResult correctHomeWork(@Valid @RequestBody HomeworkDto homeworkDto){
        boolean result = ewcHomeworkSubmissionService.correctHomeWork(homeworkDto);
        if(result){
            return AjaxResult.success("提交成功" );
        }
        return AjaxResult.error("提交失败");
    }
}
